<?php

namespace app\admin\library;

use app\admin\model\cms\Channels;
use app\admin\model\UserGroup;
use fast\Date;
use think\Db;

class CountDashboard
{

    public static function handleSelectTimeParam($param)
    {
        $logintime = $param['select_time'] ?? ' - ';
        $logintime = explode(' - ', $logintime);
        if (!isset($logintime[0]) || !$logintime[0]) {
            $logintime[0] = Date::unixtime();//获取当天时间 如: Y-m-d 00:00:00
        }
        if (!isset($logintime[1]) || !$logintime[1]) {
            $logintime[1] = Date::unixtime('day', 0, 'end');//获取当天时间 如: Y-m-d 23:59:59
        }
        return $logintime;
    }

    //游客数量
    public static function getclient_1($param)
    {
        $logintime = self::handleSelectTimeParam($param);
         $res['name'] = '游客数量';
        $res['count'] =  Db::name('user')->where('group_id', 1)->where('logintime', 'between', [strtotime($logintime[0] . '00:00'), strtotime($logintime[1] . ' 23:59:59')])->count();
        return $res;
    }

    //试用会员数量
    public static function getclient_2($param)
    {
        $logintime = self::handleSelectTimeParam($param);
         $res['name'] = '适用会员';
        $res['count'] =  Db::name('user')->where('group_id', 2)->where('logintime', 'between', [strtotime($logintime[0] . ' 00:00:00'), strtotime($logintime[1] . ' 23:59:59')])->count();
        return $res;
    }

    //正式会员数量
    public static function getclient_3($param)
    {
        $logintime = self::handleSelectTimeParam($param);
         $res['name'] = '正式会员';
        $res['count'] =  Db::name('user')->where('group_id', 3)->where('logintime', 'between', [strtotime($logintime[0] . ' 00:00:00'), strtotime($logintime[1] . ' 23:59:59')])->count();
        return $res;
    }

    //论坛板块数量
    public static function getclient_4($param)
    {
        $logintime = self::handleSelectTimeParam($param);
        $res['name'] = '论坛板块数量';
        $res['count'] =  Db::name('channels')->where('parent_id', 3)->where('is_del', 0)->where('creat_time', 'between', [strtotime($logintime[0] . ' 00:00:00'), strtotime($logintime[1] . ' 23:59:59')])->count();
        return $res;
    }

    //论坛文章数量
    public static function getclient_5($param)
    {
        $logintime = self::handleSelectTimeParam($param);
        $res['name'] = '论坛文章数量';
        $res['count'] = Db::name('plate_post')->where('creat_time', 'between', [$logintime[0], $logintime[1]])->count();
        return $res;
    }

    //新闻板块数量
    public static function getclient_6($param)
    {
        $logintime = self::handleSelectTimeParam($param);
        $res['name'] = '新闻版块数量';
        $res['count'] =  Db::name('channels')->where('parent_id', 5)->where('is_del', 0)->where('creat_time', 'between', [strtotime($logintime[0] . ' 00:00:00'), strtotime($logintime[1] . ' 23:59:59')])->count();
        return $res;
    }

    //新闻文章数量
    public static function getclient_7($param)
    {
        $logintime = self::handleSelectTimeParam($param);
        $res['name'] = '新闻内容数量';
        $res['count'] =  Db::name('news')->where('is_del', 0)->where('create_time', 'between', [strtotime($logintime[0] . ' 00:00:00'), strtotime($logintime[1] . ' 23:59:59')])->count();
        return $res;
    }

    //首页浏览ip数量
    public static function getclient_8($param)
    {
        $logintime = self::handleSelectTimeParam($param);
        $res['name'] = '首页浏览ip数量';
        $res['count'] =  Db::name('user')->where('logintime', 'between', [strtotime($logintime[0] . ' 00:00:00'), strtotime($logintime[1] . ' 23:59:59')])->group('loginip')->count('loginip');
        return $res;
    }

    //直播广场版块数量
    public static function getclient_9($param)
    {
        $logintime = self::handleSelectTimeParam($param);
        $res['name'] = '直播广场版块数量';
        $res['count'] =  Db::name('channels')->where('parent_id', 6)->where('is_del', 0)->where('creat_time', 'between', [strtotime($logintime[0] . ' 00:00:00'), strtotime($logintime[1] . ' 23:59:59')])->count();
        return $res;
    }

    //直播广场内容数量
    public static function getclient_10($param)
    {
        $logintime = self::handleSelectTimeParam($param);
        $res['name'] = '直播广场内容数量';
        $res['count'] =  Db::name('live')->where('create_time', 'between', [strtotime($logintime[0] . ' 00:00:00'), strtotime($logintime[1] . ' 23:59:59')])->count();
        return $res;
    }



    //成长计划版块数量
    public static function getclient_11($param)
    {
        $logintime = self::handleSelectTimeParam($param);
        $res['name'] = '成长计划版块数量';
        $res['count'] =  Db::name('channels')->where('parent_id', 4)->where('is_del', 0)->where('creat_time', 'between', [strtotime($logintime[0] . ' 00:00:00'), strtotime($logintime[1] . ' 23:59:59')])->count();
        return $res;
    }

    //成长计划内容数量
    public static function getclient_12($param)
    {
        $logintime = self::handleSelectTimeParam($param);
        $res['name'] = '成长计划内容数量';
        $res['count'] =  Db::name('video')->where('create_time', 'between', [strtotime($logintime[0] . ' 00:00:00'), strtotime($logintime[1] . ' 23:59:59')])->count();
        return $res;
    }


    //量化辅助(图表)版块数量
    public static function getclient_13($param)
    {
        $logintime = self::handleSelectTimeParam($param);
        $res['name'] = '量化辅助版块数量';
        $res['count'] =  Db::name('channels')->where('parent_id', 2)->where('is_del', 0)->where('creat_time', 'between', [strtotime($logintime[0] . ' 00:00:00'), strtotime($logintime[1] . ' 23:59:59')])->count();
        return $res;
    }

    //量化辅助(图表)内容数量
    public static function getclient_14($param)
    {
        $logintime = self::handleSelectTimeParam($param);
        $res['name'] = '量化辅助内容数量';
        $res['count'] =  Db::name('pro_diyform')->where('createtime', 'between', [strtotime($logintime[0] . ' 00:00:00'), strtotime($logintime[1] . ' 23:59:59')])->count();
        return $res;
    }

    //数据库版块数量
    public static function getclient_15($param)
    {
        $logintime = self::handleSelectTimeParam($param);
        $res['name'] = '数据库版块数量';
        $res['count'] =  Db::name('channels')->where('parent_id', 7)->where('is_del', 0)->where('creat_time', 'between', [strtotime($logintime[0] . ' 00:00:00'), strtotime($logintime[1] . ' 23:59:59')])->count();
        return $res;
    }

    //数据库内容数量
    public static function getclient_16($param)
    {
        $logintime = self::handleSelectTimeParam($param);
        $res['name'] = '数据库内容数量';
        $res['count'] =  Db::name('investment')->where('createtime', 'between', [strtotime($logintime[0] . ' 00:00:00'), strtotime($logintime[1] . ' 23:59:59')])->count();
        return $res;
    }

    //会员统计相关
    public static function getUserGroupCount($param){
        $user_group = UserGroup::where('status','normal')
            ->field('id,name')
            ->withCount(['user'])
            ->select();
        return $user_group;
    }
}